<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>
        <template #right>
          <el-button
            v-if="$store.state.user.userfrom.roles.points.includes('import_excel')"
            type="warning"
            size="small"
            @click="$router.push('/import')"
          >excel导入</el-button>
          <el-button
            v-if="$store.state.user.userfrom.roles.points.includes('exprot_excel')"

            type="danger"
            size="small"
<<<<<<< HEAD
            @click="outline"
=======
            @click="exprot"
>>>>>>> a9972fb9b0473e8e8d6ab928bdacb7c4e00fcd7f
          >excel导出</el-button>
          <el-button
            v-if="$store.state.user.userfrom.roles.points.includes('addpeople')"
            type="primary"
            size="small"
            @click.native="adds()"
          >新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px">
        <el-table border :data="list" :default-sort="{ prop: 'timeOfEntry' }">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像" prop="staffPhoto">
            <template slot-scope="scope">
              <ImageHolder :src="scope.row.staffPhoto" />
            </template>
          </el-table-column>

          <el-table-column label="工号" prop="workNumber" sortable />
          <el-table-column label="聘用形式" prop="formOfEmployment" />
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <!-- <el-table-column label="账户状态" /> -->
          <el-table-column label="操作" width="280">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail?id=${scope.row.id}`)">查看</el-button>
              <el-button type="text" size="small" @click="assign(scope.row.id) ">分配角色</el-button>
              <el-button
                type="text"
                size="small"
                @click="dels(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <!-- <el-pagination layout="prev, pager, next" /> -->
          <el-pagination
            :current-page="page"
            :page-sizes="[1, 5, 10, 20]"
            :page-size="size"
            layout="total, sizes, prev, pager, next"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <el-dialog
        :visible.sync="dialogVisible"
      ><Dalog
        v-if="dialogVisible"
        @success="Usuccess"
      /></el-dialog>
      <el-dialog :visible.sync="assignshow">
        <AssignRole v-if="assignshow" :cur-id="curId" @success="Success" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getinfos, dodel } from '@/api/employees'
import Dalog from './dalog.vue'
// import PageTools from '@/components/PageTools'
export default {
  components: { Dalog },
  // components: {
  //   PageTools
  // }
  data() {
    return {
      list: [],
      page: 1,
      size: 5,
      total: 0,
      dialogVisible: false,
      assignshow: false,
      curId: ''
    }
  },
  created() {
    this.getlist(

    )
  },
  methods: {
<<<<<<< HEAD
    Success() {
      this.$message.success('分配成功')
      this.assignshow = false
    },
    assign(id) {
      this.assignshow = true
      this.curId = id
    },
=======
<<<<<<< HEAD
>>>>>>> caff8b8bdcf3c98275842cc29ac8332eac55732c
    outline() {
      const map = {
        'id': '编号',
        'password': '密码',
        'mobile': '手机号',
        'username': '姓名',
        'timeOfEntry': '入职日期',
        'formOfEmployment': '聘用形式',
        'correctionTime': '转正日期',
        'workNumber': '工号',
        'departmentName': '部门',
        'staffPhoto': '头像地址'
      }
      const type_lx = { 1: '正式', 2: '非正式', 3: 'aaaa' }
      const headers = Object.keys(this.list[0])
      const header = headers.map(item => map[item])
      // console.log(headers)
      console.log(header)
      // eslint-disable-next-line no-undef
      console.log(this.list)
      // eslint-disable-next-line no-return-assign
      this.list.map(item => item.formOfEmployment = type_lx[item.formOfEmployment])
      const data = this.list.map(item => Object.values(item))
      console.log(data)
      import('@/vendor/Export2Excel').then(excel => {
        // 准备数据
        // const tHeader = ['Id', 'Title', 'Author', 'Readings', 'Date']
        // const filterVal = ['id', 'title', 'author', 'pageviews', 'display_time']
        // const list = this.list
        // const data = this.formatJson(filterVal, list)

        // 生成excel
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
=======
    exprot() {
      import('@/vendor/Export2Excel').then(excel => {
        // 准备数据
        const maps = {
          'id': '编号',
          'password': '密码',
          'mobile': '手机号',
          'username': '姓名',
          'timeOfEntry': '入职日期',
          'formOfEmployment': '聘用形式',
          'correctionTime': '转正日期',
          'workNumber': '工号',
          'departmentName': '部门',
          'staffPhoto': '头像地址'
        }
        const arr = this.list
        console.log(arr)
        const data = arr.map(item => Object.values(item))
        const header = Object.keys(arr[0]).map(item => maps[item])
        console.log(header)
        // 生成excel
        excel.export_json_to_excel({
          header, // 表头 必填
          data,
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx'
>>>>>>> a9972fb9b0473e8e8d6ab928bdacb7c4e00fcd7f
        })
        this.downloadLoading = false
      })
    },
    Usuccess() {
      this.dialogVisible = false
    },
    adds() {
      this.dialogVisible = true
    },
    dels(id) {
      this.$confirm('确定要删除嘛???').then(async() => {
        // console.log(1)
        if (this.list.length === 1 && this.page > 1) {
          this.page--
        }
        await dodel(id)
        this.$message.success('删除成功')
        this.getlist()
      }).catch(() => { })
    },
    handleSizeChange(val) {
      this.size = val
      this.page = 1
      this.getlist()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.page = val
      this.getlist()
      console.log(`当前页: ${val}`)
    },
    async getlist() {
      const { data: res } = await getinfos({ page: this.page, size: this.size })
      this.list = res.rows
      this.total = res.total
      return res
    }
  }
}
</script>
